package com.neo.controller;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.params.HttpMethodParams;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.neo.entity.CfcaReq;



@Controller
public class HelloWorldController {

	private static final Logger logger = LoggerFactory.getLogger(HelloWorldController.class);
    @RequestMapping("/hello")
    @ResponseBody
    public String index() {
        return "Hello World mifei";
    }
    
	@RequestMapping(value = "/customers/cfca_asyn", method = RequestMethod.POST)
    @ResponseBody
    public String test(@RequestBody CfcaReq cfcaReq,HttpServletRequest req, HttpServletResponse response) throws HttpException, IOException{
		logger.info("开始=======================");
		String url="http://localhost:8080/kuai/callback/loan/sign_result";
		sendHttp(url);
		try {
			Thread.sleep(500);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
        return"ok";
    }
	
	
	public static String sendHttp(String reqUrl)
			throws HttpException, IOException {

		String str = "";

		/* 构造httpClient实例 */
		HttpClient httpClient = new HttpClient();

		/* 连接超时时间 15 秒 */
		httpClient.getHttpConnectionManager().getParams()
				.setConnectionTimeout(1000 * 60);
		/* 读取超时时间 15秒 */
		httpClient.getHttpConnectionManager().getParams()
				.setSoTimeout(1000 * 60);
		/* 创建post方法实例 */
		PostMethod postMethod = new PostMethod(reqUrl);
		postMethod.getParams().setParameter(
				HttpMethodParams.HTTP_CONTENT_CHARSET, "UTF-8");

		NameValuePair[] data = new NameValuePair[2];
		data[0] = new NameValuePair("applicationno", "1");
		data[1] = new NameValuePair("username", "2");

		// 将表单的值放入postMethod中
		postMethod.setRequestBody(data);

		int code = httpClient.executeMethod(postMethod);
		if (code == 200) {
   
			System.out.println("提交结果：" + code);

			str = postMethod.getResponseBodyAsString();

			System.out.println(str);
		} else {
			/* 测试时候远程服务不存在，屏蔽此异常抛出 */
			System.out.println("提交失败：" + code);
		}

		return str;

	}
}